package online.foxnull2.foxmall.search.controller;

import lombok.extern.slf4j.Slf4j;
import online.foxnull2.common.exception.BizCodeEnum;
import online.foxnull2.common.to.es.SkuEsModel;
import online.foxnull2.common.utils.R;
import online.foxnull2.foxmall.search.service.ProductSaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/search")
public class ElasticSearchController {

    private final ProductSaveService productSaveService;

    @Autowired
    public ElasticSearchController(ProductSaveService productSaveService) {
        this.productSaveService = productSaveService;
    }

    @PostMapping("/product/save")
    public R productSave(@RequestBody List<SkuEsModel> skuEsModels) {
        Boolean res;
        try {
            res = productSaveService.productSaveService(skuEsModels);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("product up error -> {}", e.getMessage());
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnum.PRODUCT_UP_EXCEPTION.getMsg());
        }
        return !res ? R.ok() : R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnum.PRODUCT_UP_EXCEPTION.getMsg());
    }
}
